-
Notifications
You must be signed in to change notification settings - Fork 898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add internal column to service template for transformation plan #17748
Add internal column to service template for transformation plan #17748
Conversation
@bzwei @gmcculloug Please review. |
app/models/service_template.rb
Outdated
@@ -74,6 +77,7 @@ class ServiceTemplate < ApplicationRecord | |||
scope :without_service_template_catalog_id, -> { where(:service_template_catalog_id => nil) } | |||
scope :with_existent_service_template_catalog_id, -> { where.not(:service_template_catalog_id => nil) } | |||
scope :displayed, -> { where(:display => true) } | |||
scope :public_service_template, -> { where(:internal => true) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work? I don't think you can use a where
like this to query on a reserved attribute.
I tried it on another model and got:
NoMethodError: undefined method `eq' for nil:NilClass
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/predicate_builder/basic_object_handler.rb:9:in `call'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/predicate_builder.rb:76:in `build'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/predicate_builder.rb:46:in `expand'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/predicate_builder.rb:90:in `block in expand_from_hash'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/predicate_builder.rb:86:in `each'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/predicate_builder.rb:86:in `flat_map'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/predicate_builder.rb:86:in `expand_from_hash'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/predicate_builder.rb:31:in `build_from_hash'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/where_clause_factory.rb:22:in `build'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/query_methods.rb:632:in `where!'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/relation/query_methods.rb:625:in `where'
from /home/bdunne/.gem/ruby/2.4.3/gems/activerecord-5.0.7/lib/active_record/querying.rb:10:in `where'
from (irb):2
from /home/bdunne/.gem/ruby/2.4.3/gems/railties-5.0.7/lib/rails/commands/console.rb:65:in `start'
from /home/bdunne/.gem/ruby/2.4.3/gems/railties-5.0.7/lib/rails/commands/console_helper.rb:9:in `start'
from /home/bdunne/.gem/ruby/2.4.3/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:78:in `console'
from /home/bdunne/.gem/ruby/2.4.3/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /home/bdunne/.gem/ruby/2.4.3/gems/railties-5.0.7/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
app/models/service_template.rb
Outdated
@@ -75,6 +78,10 @@ class ServiceTemplate < ApplicationRecord | |||
scope :with_existent_service_template_catalog_id, -> { where.not(:service_template_catalog_id => nil) } | |||
scope :displayed, -> { where(:display => true) } | |||
|
|||
def self.public_service_templates | |||
ServiceTemplate.all - ServiceTemplate.joins(:reserved_rec).where(:reserves=> {:reserved => {:internal => true}.to_yaml}).to_a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- This should return a scope if possible
- That where clause is wrong, because there could be other things in reserves if we need to add them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please add a spec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Fryguy what is the way to write the where
clause? We tested but cannot make reserved
field deserialized in this query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we query like ServiceTemplate.includes(:reserved_rec).select { |s| !s.internal }
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Fryguy Changed back to scope based on above query.
Injecting a |
👎 for subclass...it conflates a property of potentially any service template with the type of a service template. |
app/models/service_template.rb
Outdated
@@ -74,6 +77,7 @@ class ServiceTemplate < ApplicationRecord | |||
scope :without_service_template_catalog_id, -> { where(:service_template_catalog_id => nil) } | |||
scope :with_existent_service_template_catalog_id, -> { where.not(:service_template_catalog_id => nil) } | |||
scope :displayed, -> { where(:display => true) } | |||
scope :public_service_templates, -> { includes(:reserved_rec).select { |s| s.internal.nil? } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hsong-rh You are missing the closing bracket on the scope.
app/models/service_template.rb
Outdated
@@ -1,4 +1,7 @@ | |||
class ServiceTemplate < ApplicationRecord | |||
include ReservedMixin | |||
reserve_attribute :internal, :boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines should be moved down by the other include
statements around line 38.
@@ -7,6 +7,8 @@ def request_type | |||
"transformation_plan" | |||
end | |||
|
|||
default_value_for :internal, true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would also want a default_value_for :internal, false
at the base class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really want such a default_value? Without default we don't need to insert a record in the reserved table.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer this upstream once we have the real column, so it is fine to ignore while we are working on the reserves table commits.
app/models/service_template.rb
Outdated
@@ -74,6 +77,7 @@ class ServiceTemplate < ApplicationRecord | |||
scope :without_service_template_catalog_id, -> { where(:service_template_catalog_id => nil) } | |||
scope :with_existent_service_template_catalog_id, -> { where.not(:service_template_catalog_id => nil) } | |||
scope :displayed, -> { where(:display => true) } | |||
scope :public_service_templates, -> { includes(:reserved_rec).select { |s| s.internal.nil? } } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the condition be !s.internal
to handle both nil and false values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This returns an array, but we want a true scope. The best I can come up is
where.not(:id => ServiceTemplate.includes(:reserved_rec).joins(:reserved_rec).select { |s| s.internal == true }.collect(&:id))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bzwei 👍
42d0556
to
da7a48d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
st1 = FactoryGirl.create(:service_template_transformation_plan) | ||
st2 = FactoryGirl.create(:service_template) | ||
|
||
expect(st1.internal).to be_truthy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer internal?
since it's a boolean attribute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought... Do you need to test this? It's just testing default_value_for
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they are testing the default value, to make sure if it is set correctly. internal
is column name.
|
||
expect(st1.internal).to be_truthy | ||
expect(st2.internal).to be_falsey | ||
expect(ServiceTemplate.public_service_templates.count).to eq(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two expectations can be merged into:
expect(ServiceTemplate.public_service_templates).to match_array([st2])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
da7a48d
to
d22be20
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filtering on columns that are serialized in reserves#reserved
will always bring back all records. (sure wish it were an hstore
or jsonb
column)
app/models/service_template.rb
Outdated
@@ -74,6 +76,7 @@ class ServiceTemplate < ApplicationRecord | |||
scope :without_service_template_catalog_id, -> { where(:service_template_catalog_id => nil) } | |||
scope :with_existent_service_template_catalog_id, -> { where.not(:service_template_catalog_id => nil) } | |||
scope :displayed, -> { where(:display => true) } | |||
scope :public_service_templates, -> { where.not(:id => ServiceTemplate.includes(:reserved_rec).joins(:reserved_rec).select { |s| s.internal == true }.collect(&:id)) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if the joins
is necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kbrock What if we query the Reserve first?
where.not(:id => Reserved.where(:resource_type => "ServiceTemplate").all.collect { |r| r.resource_id if r[:internal] == true }.compact)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kbrock Without join
, ServiceTemplate.includes(:reserved_rec).select { |s| s.internal == true }.collect(&:id)
give us same result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hsong-rh Remember we need to retain this as a scope so users can chain other scopes onto the resulting object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bdunne This works: where.not(:id => Reserve.where(:resource_type => "ServiceTemplate").all.collect { |r| r.resource_id if r.reserved[:internal] == true }.compact)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmcculloug yes, they return as scope.
d22be20
to
61ec522
Compare
app/models/service_template.rb
Outdated
@@ -74,6 +76,7 @@ class ServiceTemplate < ApplicationRecord | |||
scope :without_service_template_catalog_id, -> { where(:service_template_catalog_id => nil) } | |||
scope :with_existent_service_template_catalog_id, -> { where.not(:service_template_catalog_id => nil) } | |||
scope :displayed, -> { where(:display => true) } | |||
scope :public_service_templates, -> { where.not(:id => Reserve.where(:resource_type => "ServiceTemplate").all.collect { |r| r.resource_id if r.reserved[:internal] == true }.compact) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need .all
, or even simpler
where.not(:id => Reserve.where(:resource_type => "ServiceTemplate").collect { |r| r.reserved[:internal] && r.resource_id }.compact
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bzwei Changed.
61ec522
to
ee21a01
Compare
Oh, good catch on the |
@bdunne The line is too long. Just to make it shorter. |
Too long for what? |
Previously the line for the scope is very long. I thought we can make it shorter and proposed to use |
@bdunne Do you have a preference? I feel like this PR is really close now and would like to see if we can get it to the finish line. 🏁 |
I thought |
ee21a01
to
54daf2a
Compare
@bdunne Changed as you suggested. Thanks, |
54daf2a
to
f333539
Compare
Checked commit hsong-rh@f333539 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0 |
Add internal column to service template for transformation plan (cherry picked from commit 2a66cb5) https://bugzilla.redhat.com/show_bug.cgi?id=1610729
Gaprindashvili backport details:
|
Add internal flag to limit the access scope for transformation plan.
https://bugzilla.redhat.com/show_bug.cgi?id=1594408
Schema PR to migrate away from reserves table: ManageIQ/manageiq-schema#238.